/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */

package net.stuff.servoy.beans.calendar.wicket;

import java.util.Date;

import org.apache.wicket.model.PropertyModel;

/**
 * 
 * @author servoy-stuff
 * http://www.servoy-stuff.net/
 */
public class DateStringPropertyModel extends PropertyModel {
	
	private static final long serialVersionUID = 1L;

	public DateStringPropertyModel(Object modelObject, String expression) {
		super(modelObject, expression);
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.model.AbstractPropertyModel#setObject(java.lang.Object)
	 */
	@Override
	public void setObject(Object object) {
		Object target = getTarget();
		if (target instanceof WicketDateChooser) {
			if (object != null && object instanceof Date) {
				((WicketDateChooser)target).setDate((Date)object);
			} else {
				boolean validationMode = ((WicketDateChooser)target).getValidationEnabled();
				if (validationMode) {
					if (object != null && object instanceof String && ((String)object).trim().length() > 0) {
						super.setObject(object);
					} else {
						((WicketDateChooser)target).setDate((Date)null);
					}
				} else {
					((WicketDateChooser)target).setDate((String)object);
				}
			}
		}
	}
	
}
